package com.study.controller;

import com.study.service.AlbumItemService;
import com.study.service.RankingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : wilbur.yuan
 * @date : 2018-11-20
 */
@Slf4j
@RestController
public class RankingsController {


    @Autowired
    private RankingService rankingService;

    @Autowired
    private AlbumItemService albumItemService;


    @GetMapping("/rankings")
    public Result<Rankings> rankings(@RequestParam String albumId, @RequestParam int limit){
        log.info("RankingsController.rankings req : albumId : {}, limit : {}" , albumId ,limit);

        limit = limit > 30 ? 30 : limit;

        albumItemService.validateAlbum(albumId);

        Rankings result = new Rankings();

        result.setRankings(rankingService.queryRankings(albumId, limit));

        result.setSum(rankingService.querySum(albumId));

        return Result.success(result);
    }



}
